"""
Pydantic 错误消息汇总
"""

# flake8: noqa

from typing import (
    Dict,
    Type,
)

from utils.validator_utils.pydantic_x.errors.base import PydanticErrorBase
from utils.validator_utils.pydantic_x.version import PYDANTIC_V1

if PYDANTIC_V1:
    from utils.validator_utils.pydantic_x.errors.v1_errors import (
        PydanticV1BuiltInError as PydanticBuiltInError,
        PydanticV1CustomErrorBase as PydanticCustomErrorBase,
    )

else:
    from utils.validator_utils.pydantic_x.errors.v2_errors import (
        PydanticV2BuiltInError as PydanticBuiltInError,
        PydanticV2CustomErrorBase as PydanticCustomErrorBase,
    )


def get_code_2_error() -> Dict[str, Type[PydanticErrorBase]]:
    """ 获取 Pydantic 错误编号 - 错误类 映射 """

    return {
        f"{cls.code_prefix}{cls.code}": cls
        for cls in find_all_subclasses(PydanticErrorBase)
        if cls.code
    }


def find_all_subclasses(cls) -> list:
    """ 递归查找所有子类 """

    subclasses = set()

    def recurse(base_class):
        for subclass in base_class.__subclasses__():
            subclasses.add(subclass)
            recurse(subclass)

    recurse(cls)

    return list(subclasses)
